//
//  WoDeShouRuViewController.m
//  EJLX
//
//  Created by adc on 15/11/17.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "WoDeShouRuViewController.h"
#import "FirstWoDeShouRuTableViewCell.h"
#import "SecondWoDeShouRuTableViewCell.h"
#import "ThireWoDeShouRuTableViewCell.h"
#import "FourWoDeShouRuTableViewCell.h"
#import "FirveWoDeShouRuTableViewCell.h"
#import "SixWoDeShouRuTableViewCell.h"
#import "ZhangDanMingXiViewController.h"
#import "FenRunXiangQingViewController.h"
#import "WeiDianLeiJiShouRuViewController.h"
#import "HomeVC.h"
#import "HttpsManager.h"
#import "TwoWoDeShouRu.h"
#import "JieSuanViewController.h"
#import "WeiDianLeiJiShouRuViewController.h"
@interface WoDeShouRuViewController ()

@end

@implementation WoDeShouRuViewController
-(void)viewWillAppear:(BOOL)animated
{
    [HttpsManager requestGetWODeShouRu:^(WoDeShouRu *shouRu)
     {
         if (shouRu!=nil)
         {
             _woDe=shouRu;
             _listAry=_woDe.listAry;
             __weak WoDeShouRuViewController *weakSelf = self;
             [_tableView addPullToRefreshWithActionHandler:^{
                 NSLog(@"当进行下拉刷新执行该方法");
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             
             [_tableView addInfiniteScrollingWithActionHandler:^{
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];

             [_tableView reloadData];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
         
     }];

}
- (void)requestEnd{
    [_tableView.pullToRefreshView stopAnimating];
    
    [_tableView.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor=[UIColor whiteColor];
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE , 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35, 10, 70, 24)];
    label.text=@"我的收入";
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
    UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [but1 setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but1];
    UIButton*but2=[[UIButton alloc]initWithFrame:CGRectMake(WIDE-70,20,80, 24)];
    [but2 setTitle:@"账单明细" forState:UIControlStateNormal];
    but2.font=[UIFont systemFontOfSize:10];
    [but2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but2 addTarget:self action:@selector(butk2) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but2];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDE,HEIGHT-64) style:UITableViewStylePlain];
    _tableView.rowHeight=70;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    //_tableView.backgroundColor=[UIColor redColor];
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"FirstWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"SecondWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    [_tableView registerNib:[UINib nibWithNibName:@"ThireWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
    [_tableView registerNib:[UINib nibWithNibName:@"FourWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
    [_tableView registerNib:[UINib nibWithNibName:@"FirveWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell5"];
    [_tableView registerNib:[UINib nibWithNibName:@"SixWoDeShouRuTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell6"];
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    if (section==1)
    {
        return 3;
    }
    
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1+1+_listAry.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==1)
    {
        if (indexPath.row==0)
        {
            WeiDianLeiJiShouRuViewController*vc=[[WeiDianLeiJiShouRuViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        if (indexPath.row==2)
        {
            FenRunXiangQingViewController*vc=[[FenRunXiangQingViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (_listAry.count>1)
    {
        if (indexPath.section>1)
        {
            TwoWoDeShouRu*two=_listAry[indexPath.section-2];
            if (indexPath.row==1)
            {
                JieSuanViewController*vc=[[JieSuanViewController alloc]init];
                vc.accountId=[NSString stringWithFormat:@"%d",two.accountId];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }

    }
    else
    {
        LoginVC*vc=[[LoginVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
  }
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        FirstWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell.yuE.text=[NSString stringWithFormat:@"%.2f",_woDe.banlance*0.01];
       
        cell.shouRu.text=[NSString stringWithFormat:@"%.2f",_woDe.acc_balance*0.01];
        
        return cell;
    }
//    if (indexPath.section==1)
//    {
//        ThireWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
//        NSArray*ary=[NSArray arrayWithObjects:@"微店累计收入",@"o2o累计收入",@"分润累计收入", nil];
//        cell.label.text=ary[indexPath.row];
//        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//        if (indexPath.row==0)
//        {
//            cell.moneyLabel.text=[NSString stringWithFormat:@"%lld",_woDe.acc_shop_balance];
//        }
//        if (indexPath.row==1)
//        {
//            cell.moneyLabel.text=[NSString stringWithFormat:@"%lld",_woDe.acc_o2o_balance];
//        }
//        if (indexPath.row==2)
//        {
//            cell.moneyLabel.text=[NSString stringWithFormat:@"%lld",_woDe.acc_profit_balance];
//        }
//        return cell;
//        
//    }
    
    
    
    
    if (indexPath.section>1)
    {
        if (_listAry.count>1)
        {
            TwoWoDeShouRu*two=_listAry[indexPath.section-2];
            if (indexPath.row==0)
            {
                
                FirveWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell5" forIndexPath:indexPath];
                cell.label1.text=two.accountTitle;
                cell.label2.text=[NSString stringWithFormat:@"%.2f",two.accountAmount*0.01];
                return cell;
            }
            SixWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell6" forIndexPath:indexPath];
            // [cell.but setBackgroundColor:RGB_COLOR(0x00c49f)];
            cell.butLabel.backgroundColor=RGB_COLOR(0x00c49f);
            cell.butLabel.text=@"结算";
            cell.butLabel.textAlignment=NSTextAlignmentCenter;
            cell.butLabel.textColor=[UIColor whiteColor];
            // cell.but.tag=indexPath.section;
            //[cell.but addTarget:self action:@selector(jieSuanBut:) forControlEvents:UIControlEventTouchUpInside];
            cell.label2.text=[NSString stringWithFormat:@"%.2f",two.settlement*0.01];
            return cell;

        }
        else
        {
            if (indexPath.row==0)
            {
                
                FirveWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell5" forIndexPath:indexPath];
                cell.label1.text=@"0";
                cell.label2.text=@"0";
                return cell;
            }
            SixWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell6" forIndexPath:indexPath];
            // [cell.but setBackgroundColor:RGB_COLOR(0x00c49f)];
            cell.butLabel.backgroundColor=RGB_COLOR(0x00c49f);
            cell.butLabel.text=@"结算";
            cell.butLabel.textAlignment=NSTextAlignmentCenter;
            cell.butLabel.textColor=[UIColor whiteColor];
            // cell.but.tag=indexPath.section;
            //[cell.but addTarget:self action:@selector(jieSuanBut:) forControlEvents:UIControlEventTouchUpInside];
            cell.label2.text=@"0";
            return cell;

            
            
            
        }
        
    }
    ThireWoDeShouRuTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
    NSArray*ary=[NSArray arrayWithObjects:@"微店累计收入",@"o2o累计收入",@"分润累计收入", nil];
    cell.label.text=ary[indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row==0)
    {
        cell.moneyLabel.text=[NSString stringWithFormat:@"%.2f",_woDe.acc_shop_balance*0.01];
    }
    if (indexPath.row==1)
    {
        cell.moneyLabel.text=[NSString stringWithFormat:@"%.2f",_woDe.acc_o2o_balance*0.01];
    }
    if (indexPath.row==2)
    {
        cell.moneyLabel.text=[NSString stringWithFormat:@"%.2f",_woDe.acc_profit_balance*0.01];
    }
    return cell;
    
}
-(void)jieSuanBut:(UIButton*)sender
{
    //UIView*jieSuanView=[UIView alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
}
-(void)weiDianLeiJiBut
{
    WeiDianLeiJiShouRuViewController*vc=[[WeiDianLeiJiShouRuViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//返回按钮
-(void)butClick
{
   
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"****");
}
-(void)fenRunXiangQingBut
{
    FenRunXiangQingViewController*vc=[[FenRunXiangQingViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//账单明细按钮
-(void)butk2
{
    ZhangDanMingXiViewController*zhangDan=[[ZhangDanMingXiViewController alloc]init];
    [self.navigationController pushViewController:zhangDan animated:YES];
}
//分润详情按钮
-(void)xiangQingBut
{
    FenRunXiangQingViewController*vc=[[FenRunXiangQingViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
